package com.pt25.base.dep.es.ddo;

import com.pt25.base.util.JsonUtil;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Mapping {
    private String index;
    private Integer shards = 1;
    private Integer replicas = 1;
    private List<MappingProperty> properties;


    public String createMapping() {
        Map<String, Object> propertyObj = new HashMap<>();
        for (MappingProperty property : properties) {
            Map<String, Object> propertyConfig = new HashMap<>();
            propertyConfig.put("type", property.getType());
            propertyConfig.put("index", property.getIndex());
            propertyConfig.put("analyzer", property.getAnalyzer());
            propertyConfig.put("format", property.getFormat());
            propertyObj.put(property.getName(), propertyConfig);
        }
        Map<String, Object> propertiesData = new HashMap<>();
        propertiesData.put("properties", propertyObj);
        String mappingStr = JsonUtil.toJson(propertiesData);
        log.info("mapping:{}", mappingStr);
        return mappingStr;
    }

}
